<?php
namespace app\admin\controller;
use think\Db;
class Banner extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['b_model'] = $role;
            }
            if(session ('area')){
                $map['b_area_id'] = ['in',session('area')];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('banner d')
                ->join('admin a','a.id = d.b_user_id','left')
                ->where($map)
                ->count();
            $lists =  Db::name('banner d')
                ->join('admin a','a.id = d.b_user_id','left')
                ->where($map)
                ->field('d.*,a.username')
                ->page($Nowpage,$limits)
                ->order('b_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('banner/index');
    }

    public function bannerAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'b_address'=>$img,
                'b_user_id'=>session ('uid'),
                'b_type'=>session('level'),
                'b_area_id'=>session('myarea'),
                'b_model'=>$type,
                'b_add_time'=>date('Y-m-d H:i:s'),
                'b_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('banner')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('banner/add_banner');
    }


    public function bannerEdit(){
        extract (input());
        if(request()->isPost ()){
            $param = [
                'b_address'=>$img,
                'b_model'=>$type,
                'b_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('banner')->where('b_id',$id)->update ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $data = Db::name('banner')->where('b_id',$id)->find();
        return $this->fetch ('banner/edit_banner',['data'=>$data]);
    }

    public function bannerDel(){
        extract (input());
        $res = Db::name('banner')->where('b_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}